package org.weasel.unittests;

import java.sql.Time;

import junit.framework.TestCase;

import org.weasel.business.Company;
import org.weasel.business.Developer;
import org.weasel.business.IDFactory;
import org.weasel.business.Iteration;
import org.weasel.business.Project;
import org.weasel.business.Release;
import org.weasel.business.Task;
import org.weasel.business.Unscheduled;
import org.weasel.business.UserStory;
import org.weasel.data.PersistencyAdapter;

public class PersistencyAdapterTest extends TestCase {
	private String xmlTestFileLoc = "./testfolder/XMLTests.xml";
	private PersistencyAdapter persistAdapt;
	
	protected void setUp() throws Exception {
		super.setUp();
		persistAdapt  = new PersistencyAdapter();
	}

	protected void tearDown() throws Exception {
		super.tearDown();
		persistAdapt = null;
		//there is no need to delete the XML file here, since it is always overwritten
	}

	/**
	 * Creates a typical project with releases, developers, userstories (unsched and scheduled),
	 * tasks etc...
	 * @throws Exception
	 */
	public void testSaveLoad() throws Exception {
		String note = "my note";
		Company comp = new Company("Weasel Corp.", null, null);
		Developer d1 = new Developer("devMatthias", "Matthias", "Manager", "matthias@weasel.com");
		Developer d2 = new Developer("devManfred", "Manfred", "XP Programmer", "manfred@weasel.com");
		Developer d3 = new Developer("devGugu", "Gugu", "XP Programmer", "gugu@weasel.com");
		comp.addDeveloper(d1);
		comp.addDeveloper(d2);
		comp.addDeveloper(d3);
		
		//create a project
		Project p = new Project("weaselID", "Weasel", null, null, new Unscheduled(new IDFactory().getUniqueId(),null), note, "15.05.2007", 6);
		p.addDeveloper(d2);
		p.addDeveloper(d3);
		//add 2 releases and 3 iterations per release;
		for(int i=1; i<3; i++){
			Release r = new Release("release" + i, "Release " + i, null, new Unscheduled(new IDFactory().getUniqueId(),null), note + "release" + i);
			for(int j=1; j<4; j++){
				r.addIteration(new Iteration("iteration" + j, "Iteration " + j, null, note + "iteration" + j));
			}
			p.addRelease(r);
		}
		
		//add a userstory to the project unscheduled
		UserStory story1 = new UserStory("projUnschedStory", "story 1",  "03.04.2007", null, "description of story 1", Time.valueOf("00:05:00"), Time.valueOf("00:05:00"), 3, 5, false, null, null);
		p.getUnscheduled().addUserStory(story1);
		//add a userstory to the 2nd iteration of the projects 1st release + assign 1 developer and 3 tasks
		UserStory story2 = new UserStory("scheduledStoryID", "story 2",  "03.04.2007", null, "description of story 2", Time.valueOf("00:03:00"), Time.valueOf("00:01:00"), 3, 10, true, null, null);
		story2.addDeveloper(d3);
		for(int i=1; i<4; i++){
			story2.addTask(new Task("task"+i, "Description of task " + i, false));
		}
		p.getRelease("release1").getIteration("iteration2").addUserStory(story2);
		
		//add the project to the company
		comp.addProject(p);
		
		//save it to the XML file
		persistAdapt.save(xmlTestFileLoc, comp);
		
		//read the data back and check if it is identical
		Company compCheck = persistAdapt.load(xmlTestFileLoc);
		
		assertEquals("Companyname should be equal", comp.getName(), compCheck.getName());
		assertEquals("Check 2nd developer", comp.getDeveloper("devManfred").getId(), compCheck.getDeveloper("devManfred").getId());
		assertEquals("Size of the developer lists",comp.getDevelopers().size(), compCheck.getDevelopers().size());
		//retrieving correct project
		Project pCheck = compCheck.getProject("weaselID");
		assertEquals("project id", p.getId(), pCheck.getId());
		assertEquals("project name", p.getName(), pCheck.getName());
		assertEquals("project notes",p.getNotes(), pCheck.getNotes());
		assertEquals("project start date", p.getStartDate(), pCheck.getStartDate());
		assertEquals("iteration days", p.getIterationDays(), pCheck.getIterationDays());
		
		assertEquals("unscheduled userstory of project", p.getUnscheduled().getUserStory("projUnschedStory").getId(), pCheck.getUnscheduled().getUserStory("projUnschedStory").getId());
		//retrieve the scheduled userstory of 1stRelease / 2nd Iteration
		UserStory storyCheck = pCheck.getRelease("release1").getIteration("iteration2").getUserStory("scheduledStoryID");
		assertEquals("userstory of 1stRelease/2ndIteration", story2.getId(), storyCheck.getId());
		assertEquals("tasklist size of scheduled story", story2.getTasks().size(), storyCheck.getTasks().size());
		assertEquals("developer of scheduled story", story2.getDeveloper("devGugu").getId(), storyCheck.getDeveloper("devGugu").getId());
		assertEquals("expected true", story2.isExperimental(), storyCheck.isExperimental());
		
		assertEquals("release notes", p.getRelease("release1").getNotes(), pCheck.getRelease("release1").getNotes());
		assertEquals("iteration notes", p.getRelease("release1").getIteration("iteration2").getNotes(), pCheck.getRelease("release1").getIteration("iteration2").getNotes());
	}

}

